package com.ltc.cdr.tests;

import java.io.File;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

import com.ltc.cdr.domain.Cdr_volume;

public class Inscdrv {

	/**
	 * @param args
	 */
	
	 private static SessionFactory factory; 
	
	@SuppressWarnings("deprecation")
	public static void main(String[] args) {
		String da1 = "D:/cdr2g3g4g/CDRData";
		
		Configuration cfg = new Configuration();
		cfg.configure("Hibernate.cfg.xml");
		SessionFactory sf = cfg.buildSessionFactory();
		Session s = sf.openSession();
		
		
		try{
	         factory = new Configuration().configure().buildSessionFactory();
	      }catch (Throwable ex) { 
	         System.err.println("Failed to create sessionFactory object." + ex);
	         throw new ExceptionInInitializerError(ex); 
	      }
		
			
		readindirector readdrection = new readindirector();
		File f = new File(da1);
		
		List<String> filepaths = readdrection.findFiles(f);
		for (String path:filepaths){
			if(path!=null && !path.contains("gData")){
				List<Cdr_volume> cdr = readdrection.txtread(path);
				for(Cdr_volume cdrFromText:cdr){
					Cdr_volume cdr_volume = (Cdr_volume)s.get(Cdr_volume.class, cdrFromText.getServedmsidsn()); 
					if(cdr_volume!=null){
						
						Integer tmpVoldnlk=cdr_volume.getVoldnlk()+cdrFromText.getVoldnlk();
						Integer tmpVoluplk=cdr_volume.getVoluplk()+cdrFromText.getVoluplk();
						Integer duration=cdr_volume.getDurations()+cdrFromText.getDurations();
						String changeTime=cdrFromText.getChangetime();
						String recordTime=cdrFromText.getRecordopeningtime();
//						cdr_volume.setVoldnlk(tmpVoldnlk);
//						cdr_volume.setVoluplk(tmpVoluplk);
//						cdr_volume.setDurations(duration);
//						cdr_volume.setRecordopeningtime(recordTime);
//						cdr_volume.setChangetime(changeTime);
//						cdr_volume.setFilename("b20140102.155823.7638.dat.txt");
						//s.update(cdr_volume);
						
						updateCDRVOLUME(cdrFromText.getServedmsidsn(), changeTime, recordTime, tmpVoldnlk, tmpVoluplk, duration);
						     				         
					}else{
//						cdr_volume = new Cdr_volume();
//						cdr_volume.setServedmsidsn(cdrFromText.getServedmsidsn());
//						cdr_volume.setServedimsi(cdrFromText.getServedimsi());
//						cdr_volume.setServedimei(cdrFromText.getServedimei());
//						cdr_volume.setVoluplk(cdrFromText.getVoluplk());
//						cdr_volume.setVoldnlk(cdrFromText.getVoldnlk());
//						cdr_volume.setChangetime(cdrFromText.getChangetime());
//						cdr_volume.setRecordopeningtime(cdrFromText.getRecordopeningtime());
//						cdr_volume.setDurations(cdrFromText.getDurations());
//						cdr_volume.setFilename("b20140102.155823.7638.dat.txt");
//						s.save(cdr_volume);
						 addCDRVol(cdrFromText.getServedmsidsn(), cdrFromText.getServedimsi(), cdrFromText.getServedimei(), cdrFromText.getChangetime(), cdrFromText.getRecordopeningtime(), cdrFromText.getVoldnlk(), cdrFromText.getVoluplk(), cdrFromText.getDurations());
					  }
					
					
				}
//				s.flush();
//				tx.commit();

				s.close();
			}
			
			
		}
			
		
		
		/*
		cdr_volume cdr = readindirector.txtread());
		
		
		
		// Get all CDR list
		//List cdrVolList = s.createQuery("FROM cdr_volume").list(); 
		
		// Exemple update volume "8562050000009"
		cdr_volume cdr_volume = (cdr_volume)s.get(cdr_volume.class, cdr.getServedmsidsn()); 
		if(cdr_volume!=null){
			Integer tmpVoldnlk=cdr_volume.getVoldnlk()+cdr.getVoldnlk();
			Integer tmpVoluplk=cdr_volume.getVoluplk()+567;
			Integer duration=cdr_volume.getDurations()+234;
			String changeTime="140102151449+0700";
			String recordTime="140102151449+0700";
			cdr_volume.setVoldnlk(tmpVoldnlk);
			cdr_volume.setVoluplk(tmpVoluplk);
			cdr_volume.setDurations(duration);
			cdr_volume.setRecordopeningtime(recordTime);
			cdr_volume.setChangetime(changeTime);
			s.update(cdr_volume);
		}else{
			cdr_volume = new cdr_volume();
			cdr_volume.setServedmsidsn("8562050000009");
			cdr_volume.setServedimsi("457010000000000");
			cdr_volume.setServedimei("131860000000000");
			cdr_volume.setVoluplk(21902);
			cdr_volume.setVoldnlk(18843);
			cdr_volume.setChangetime("1401021514490+700");
			cdr_volume.setRecordopeningtime("1401021514490+700");
			cdr_volume.setDurations(1234);
			s.save(cdr_volume);
		}
		s.flush();
		tx.commit();
		s.close();
		
	*/
		
		
	}
	
	/* Method to add new CDRVOLUME */
	   public static void addCDRVol(String phonenum, String Servedimsi, String Servedimei, String Chtime, String Recordopeningtime, int tmpVoldnlk, int tmpVoluplk , int duration){
	      Session session = factory.openSession();
	      Transaction tx = null;
	      try{
	         tx = session.beginTransaction();
	          Cdr_volume cdr_volume = new Cdr_volume();
				cdr_volume.setServedmsidsn(phonenum);
				cdr_volume.setServedimsi(Servedimsi);
				cdr_volume.setServedimei(Servedimei);
				cdr_volume.setVoluplk(tmpVoluplk);
				cdr_volume.setVoldnlk(tmpVoldnlk);
				cdr_volume.setChangetime(Chtime);
				cdr_volume.setRecordopeningtime(Recordopeningtime);
				cdr_volume.setDurations(duration);
				cdr_volume.setFilename("b20140102.155823.7638.dat.txt");
				session.save(cdr_volume);
	         tx.commit();
	      }catch (HibernateException e) {
	         if (tx!=null) tx.rollback();
	         e.printStackTrace(); 
	      }finally {
	         session.close(); 
	      }
	    
	   }
	
	
	 /* Method to UPDATE CDRVOLUME */
	   public static void updateCDRVOLUME(String phonenum, String Chtime, String Recordopeningtime, int tmpVoldnlk, int tmpVoluplk , int duration ){
	      Session session = factory.openSession();
	      Transaction tx = null;
	      try{
	         tx = session.beginTransaction();
	         Cdr_volume cdr_volume = (Cdr_volume)session.get(Cdr_volume.class, phonenum);
				String changeTime=Chtime;
				String recordTime=Recordopeningtime;
				cdr_volume.setVoldnlk(tmpVoldnlk);
				cdr_volume.setVoluplk(tmpVoluplk);
				cdr_volume.setDurations(duration);
				cdr_volume.setRecordopeningtime(recordTime);
				cdr_volume.setChangetime(changeTime);
				cdr_volume.setFilename("b20140102.155823.7638.dat.txt");
				session.update(cdr_volume);
	         tx.commit();
	      }catch (HibernateException e) {
	         if (tx!=null) tx.rollback();
	         e.printStackTrace(); 
	      }finally {
	         session.close(); 
	      }
	   }
		
	
}
